From: Aron Xu Date: Mon, 8 Aug 2016 06:36:18 +0000 (+0800) Subject: Imported Upstream version 1.0.4 X-Git-Tag: archive/raspbian/1.1.9+ds1-4+rpi1^2^2~15^2~17 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https://%22%22/%22http:/www.example.com/cgi/%22https:/%22%22?a=commitdiff_plain;h=8f0dc491a214e37446e6678bb97627c0efb42ea0;p=opencc.git Imported Upstream version 1.0.4 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index a0c3747..5ff33eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ set (PACKAGE_URL https://github.com/BYVoid/Opencc) set (PACKAGE_BUGREPORT https://github.com/BYVoid/Opencc/issues) set (OPENCC_VERSION_MAJOR 1) set (OPENCC_VERSION_MINOR 0) -set (OPENCC_VERSION_REVISION 3) +set (OPENCC_VERSION_REVISION 4) if (CMAKE_BUILD_TYPE MATCHES Debug) set (version_suffix .Debug) diff --git a/Makefile b/Makefile index 297e029..25d7c64 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ # limitations under the License. # +PREFIX = /usr + .PHONY: build clean node test xcode-build build: @@ -24,12 +26,13 @@ build: -DBUILD_DOCUMENTATION:BOOL=ON \ -DENABLE_GTEST:BOOL=OFF \ -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_INSTALL_PREFIX=${PREFIX} \ ../..) - make -C build/rel VERBOSE=${VERBOSE} + make -C build/rel VERBOSE=${VERBOSE} PREFIX=${PREFIX} package: build make -C build/rel package_source VERBOSE=${VERBOSE} + make -C build/rel package_source VERBOSE=${VERBOSE} PREFIX=${PREFIX} test: mkdir -p build/dbg/root @@ -65,4 +68,4 @@ clean: rm -rf build xcode install: build - make -C build/rel install VERBOSE=${VERBOSE} + make -C build/rel install VERBOSE=${VERBOSE} PREFIX=${PREFIX} diff --git a/NEWS.md b/NEWS.md index 8fd420a..90553e4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,14 @@ # Change History of OpenCC +## Version 1.0.4 + +2016年4月1日 + +* 使編譯時的腳本兼容Python 3。 +* 修正Visual C++ 2015的編譯問題。 +* 增補臺灣、香港地區用字用詞轉換。 +* 更新nan以修正Node.js擴展編譯兼容性問題。 + ## Version 1.0.3 2015年7月22日 diff --git a/README.md b/README.md index 18de2d2..1ff2e91 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Open Chinese Convert (OpenCC, 開放中文轉換) is an opensource project for c * 嚴格審校一簡對多繁詞條,原則爲「能分則不合」。 * 支持中國大陸、臺灣、香港異體字和地區習慣用詞轉換,如「裏」「裡」、「鼠標」「滑鼠」。 * 詞庫和函數庫完全分離,可以自由修改、導入、擴展。 -* 支持C、C++、Python、PHP、Java、Ruby、Node.js。 +* 支持C、C++、Python、PHP、Java、Ruby、Node.js and Android。 * 兼容Windows、Linux、Mac平臺。 ### Links 相關鏈接 @@ -66,13 +66,20 @@ https://bintray.com/byvoid/opencc/OpenCC ### Build with CMake -Linux/OSX (gcc 4.6 or clang 3.2 is required): +Linux (gcc 4.6 is required): ``` make sudo make install ``` +Mac OS X (clang 3.2 is required): + +``` +make PREFIX=/usr/local +sudo make PREFIX=/usr/local install +``` + Windows MSYS: ``` @@ -87,7 +94,15 @@ cmake .. -G "Visual Studio 12" -DCMAKE_INSTALL_PREFIX="" -DCMAKE_BUILD_TYPE=Rele make ``` -### Projects using Opencc 使用OpenCC的項目 +### iOS + +See https://github.com/gelosie/OpenCC/tree/master/iOS + +### Android + +See [android-opencc](https://github.com/qichuan/android-opencc) + +## Projects using Opencc 使用OpenCC的項目 * [ibus-pinyin](http://code.google.com/p/ibus/) * [fcitx](http://code.google.com/p/fcitx/) @@ -96,6 +111,8 @@ make * [ibus-libpinyin](https://github.com/libpinyin/ibus-libpinyin) * [BYVBlog](https://github.com/byvoid/byvblog) * [豆瓣同城微信](http://weixinqiao.com/douban-event/) +* [alfred-chinese-converter](https://github.com/amowu/alfred-chinese-converter) +* [GoldenDict](https://github.com/goldendict/goldendict) ## License 許可協議 @@ -109,6 +126,10 @@ Apache License 2.0 All these libraries are statically linked. +## Change History 版本歷史 + +https://github.com/BYVoid/OpenCC/blob/master/NEWS.md + ## Contributors 貢獻者 * [BYVoid](http://www.byvoid.com/) @@ -134,3 +155,15 @@ All these libraries are statically linked. * [吕旭东](https://github.com/lvxudong) * [Weng Xuetian](https://github.com/wengxt) * [Ma Tao](https://github.com/iwater) +* [Heinz Wiesinger](https://github.com/pprkut) +* [J.W](https://github.com/jakwings) +* [Amo Wu](https://github.com/amowu) +* [Mark Tsai](https://github.com/mxgit1090) +* [Zhe Wang](https://github.com/0x1997) +* [sgqy](https://github.com/sgqy) +* [Qichuan (Sean) ZHANG](https://github.com/qichuan) +* [Flandre Scarlet](https://github.com/XadillaX) +* [宋辰文](https://github.com/songchenwen) +* [iwater](https://github.com/iwater) + +Please update this list you have contributed OpenCC. diff --git a/data/dictionary/HKVariants.txt b/data/dictionary/HKVariants.txt index 635ac2a..2660bca 100644 --- a/data/dictionary/HKVariants.txt +++ b/data/dictionary/HKVariants.txt @@ -48,6 +48,7 @@ 膃 腽 臥 卧 臺 台 +菸 煙 蒕 蒀 蔥 葱 蔿 蒍 diff --git a/data/dictionary/STCharacters.txt b/data/dictionary/STCharacters.txt index 0183381..b937f52 100644 --- a/data/dictionary/STCharacters.txt +++ b/data/dictionary/STCharacters.txt @@ -2846,6 +2846,10 @@ 龚 龔 龛 龕 龟 龜 +鿎 䃮 +鿏 䥑 +鿒 鿓 +鿔 鎶 𠀾 𠁞 𠆲 儣 𠆿 𠌥 @@ -3874,3 +3878,9 @@ 𫠒 鱆 𫠖 𩿅 𫠜 齯 +𬬭 錀 +𬬻 鑪 +𬭊 𨧀 +𬭛 𨨏 +𬭳 𨭎 +𬭶 𨭆 diff --git a/data/dictionary/TSCharacters.txt b/data/dictionary/TSCharacters.txt index 9bc2f27..ef0629d 100644 --- a/data/dictionary/TSCharacters.txt +++ b/data/dictionary/TSCharacters.txt @@ -55,6 +55,7 @@ 䁪 𥇢 䁻 䀥 䂎 𥎝 +䃮 鿎 䅐 𫀨 䅳 𫀬 䆉 𫁂 @@ -118,6 +119,7 @@ 䤤 𫟺 䥄 𫠀 䥇 䦂 +䥑 鿏 䥗 𫔋 䥩 𨱖 䥯 𫔆 @@ -2874,6 +2876,7 @@ 鋶 锍 鋸 锯 鋼 钢 +錀 𬬭 錁 锞 錂 𨱋 錄 录 @@ -2967,6 +2970,7 @@ 鎲 镋 鎳 镍 鎵 镓 +鎶 鿔 鎷 𨰾 鎸 镌 鎿 镎 @@ -3046,6 +3050,7 @@ 鑠 铄 鑣 镳 鑥 镥 +鑪 𬬻 鑭 镧 鑰 钥 鑱 镵 @@ -3828,6 +3833,7 @@ 龭 𩨎 龯 𨱆 鿁 䜤 +鿓 鿒 𠁞 𠀾 𠌥 𠆿 𠏢 𠉗 @@ -4025,15 +4031,19 @@ 𨥛 𨱀 𨥟 𫓫 𨦫 䦀 +𨧀 𬭊 𨧜 䦁 𨧰 𫟽 𨧱 𨱊 +𨨏 𬭛 𨨛 𫓼 𨨢 𫓿 𨩰 𫟾 𨪕 𫓮 𨫒 𨱐 𨬖 𫔏 +𨭆 𬭶 +𨭎 𬭳 𨭖 𫔑 𨭸 𫔐 𨮂 𨱕 diff --git a/data/dictionary/TWPhrasesIT.txt b/data/dictionary/TWPhrasesIT.txt index 21f2c5b..4722568 100644 --- a/data/dictionary/TWPhrasesIT.txt +++ b/data/dictionary/TWPhrasesIT.txt @@ -46,12 +46,15 @@ SQL注入攻擊 SQL隱碼攻擊 內核 核心 內置 內建 內聯函數 行內函數 +全局 全域性 全角 全形 兼容 相容 冒泡排序 氣泡排序 +函數 函式 函數式編程 函數語言程式設計 刀片服務器 刀鋒伺服器 分佈式 分散式 +分區 分割槽 分辨率 解析度 刷新 重新整理 刻錄 燒錄 @@ -63,6 +66,8 @@ SQL注入攻擊 SQL隱碼攻擊 半角 半形 博客 部落格 卸載 解除安裝 +原代碼 原始碼 +參數 引數 參數表 參數列 句柄 控制代碼 可視化 視覺化 @@ -74,13 +79,15 @@ SQL注入攻擊 SQL隱碼攻擊 哈希 雜湊 單片機 微控制器 回調 回撥 +固件 韌體 圖像 影象 圖庫 相簿 圖標 圖示 在線 線上 地址 地址 位址 地址欄 位址列 -城域王 都會網路 +城域網 都會網路 +堆棧 堆疊 場效應管 場效電晶體 壁紙 桌布 壁紙 外置 外接 @@ -91,12 +98,15 @@ SQL注入攻擊 SQL隱碼攻擊 字庫 字型檔 字段 欄位 字符 字元 +字符串 字串 字符集 字符集 字節 位元組 字體 字型 存儲 儲存 存盤 存檔 +宏 巨集 宏內核 單核心 +寄存器 暫存器 密鑰 金鑰 實例 例項 實例 實模式 真實模式 @@ -109,9 +119,11 @@ SQL注入攻擊 SQL隱碼攻擊 導入 匯入 導出 匯出 局域網 區域網 +局部 區域性 屏幕 螢幕 屏蔽 遮蔽 嵌套 巢狀 +布爾 布林 帶寬 頻寬 引導程序 載入程式 彙編 彙編 組譯 @@ -125,16 +137,19 @@ SQL注入攻擊 SQL隱碼攻擊 打印 列印 打印機 印表機 打開 開啟 打開 +拋出 丟擲 持久性 永續性 +指針 指標 捲積 摺積 掃描儀 掃描器 掛斷 結束通話 採樣 取樣 採樣率 取樣率 接口 介面 -控件 控制項 +控件 控制元件 插件 外掛 搜索 搜尋 +操作數 運算元 操作系統 作業系統 擴展 擴充套件 擴展名 副檔名 @@ -176,6 +191,7 @@ SQL注入攻擊 SQL隱碼攻擊 查看 檢視 桌面型 桌上型 構造函數 建構函式 +標識符 識別符號 模塊 模組 模擬 模擬 類比 模擬電子 類比電子 @@ -185,12 +201,14 @@ SQL注入攻擊 SQL隱碼攻擊 死機 宕機 殺毒 防毒 比特 位元 +比特幣 比特幣 比特率 位元率 波分複用 波長分波多工 消息 訊息 消息 添加 新增 源代碼 原始碼 源文件 原始檔 +源碼 原始碼 溢出 溢位 演示文稿 簡報 激光 鐳射 @@ -241,6 +259,7 @@ SQL注入攻擊 SQL隱碼攻擊 組件 元件 綁定 繫結 網上鄰居 網路上的芳鄰 +網卡 網絡卡 網吧 網咖 網絡 網路 網關 閘道器 @@ -255,6 +274,7 @@ SQL注入攻擊 SQL隱碼攻擊 聯繫 聯絡 聯繫歷史 通話記錄 聲卡 音效卡 +聲明 宣告 脫機 離線 腳本 指令碼 自動轉屏 自動旋轉螢幕 @@ -266,7 +286,7 @@ SQL注入攻擊 SQL隱碼攻擊 藍牙 藍芽 虛函數 虛擬函式 虛擬機 虛擬機器 -表達式 表示式 +表達式 表示式 運算式 複印 影印 複選按鈕 覈取按鈕 複選框 覈取方塊 @@ -288,16 +308,19 @@ SQL注入攻擊 SQL隱碼攻擊 調用 呼叫 調色板 調色盤 調製解調器 數據機 -調試 偵錯 +調試 除錯 偵錯 調試器 偵錯程式 變量 變數 軟件 軟體 軟驅 軟碟機 通信 通訊 通訊卡 通話卡 +通配符 萬用字元 連接 連線 +連接器 聯結器 進制 進位制 進程 程序 進程 +運算符 運算子 運行 執行 過程式編程 程序式程式設計 遞歸 遞迴 @@ -327,9 +350,12 @@ SQL注入攻擊 SQL隱碼攻擊 音頻 音訊 頁眉 頁首 頁腳 頁尾 +項目 專案 +預處理器 前處理器 頭文件 標頭檔案 頻分多址 分頻多重進接 頻分複用 分頻多工 +類型 型別 類模板 類别範本 顯像管 映象管 顯卡 顯示卡 @@ -347,4 +373,3 @@ SQL注入攻擊 SQL隱碼攻擊 默認值 預設值 點擊 點選 鼠標 滑鼠 -比特幣 比特幣 diff --git a/data/dictionary/TWPhrasesName.txt b/data/dictionary/TWPhrasesName.txt index 20c7ec5..6b0b3b6 100644 --- a/data/dictionary/TWPhrasesName.txt +++ b/data/dictionary/TWPhrasesName.txt @@ -71,6 +71,7 @@ 肯尼亞 肯亞 莫桑比克 莫三比克 萊索托 賴索托 +萬象 永珍 蘇里南 蘇利南 貝寧 貝南 贊比亞 尚比亞 diff --git a/data/scripts/find_target.py b/data/scripts/find_target.py index 4953bbb..28ada27 100755 --- a/data/scripts/find_target.py +++ b/data/scripts/find_target.py @@ -5,7 +5,7 @@ from common import find_target_items if len(sys.argv) != 3: print("Find the value keyword in all pairs") - print("Usage: ", sys.argv[0], "[input] [keyword]") + print(("Usage: ", sys.argv[0], "[input] [keyword]")) exit(1) find_target_items(sys.argv[1], sys.argv[2]) diff --git a/data/scripts/merge.py b/data/scripts/merge.py index fcbe588..170bf60 100755 --- a/data/scripts/merge.py +++ b/data/scripts/merge.py @@ -6,7 +6,7 @@ from common import sort_items if len(sys.argv) < 4: print("Merge and sort all text dictionaries") - print("Usage: ", sys.argv[0], "[input1] [input2] ... [inputN] [output]") + print(("Usage: ", sys.argv[0], "[input1] [input2] ... [inputN] [output]")) exit(1) all_lines = [] diff --git a/data/scripts/reverse.py b/data/scripts/reverse.py index 2d2d77f..f702460 100755 --- a/data/scripts/reverse.py +++ b/data/scripts/reverse.py @@ -5,7 +5,7 @@ from common import reverse_items if len(sys.argv) != 3: print("Reverse key and value of all pairs") - print("Usage: ", sys.argv[0], "[input] [output]") + print(("Usage: ", sys.argv[0], "[input] [output]")) exit(1) reverse_items(sys.argv[1], sys.argv[2]) diff --git a/data/scripts/sort.py b/data/scripts/sort.py index fb5f5ad..8268bde 100755 --- a/data/scripts/sort.py +++ b/data/scripts/sort.py @@ -5,7 +5,7 @@ from common import sort_items if len(sys.argv) < 2: print("Sort the dictionary") - print("Usage: ", sys.argv[0], "[input] ([output])") + print(("Usage: ", sys.argv[0], "[input] ([output])")) exit(1) input = sys.argv[1] diff --git a/data/scripts/sort_all.py b/data/scripts/sort_all.py index 0f6614a..0cecdaa 100755 --- a/data/scripts/sort_all.py +++ b/data/scripts/sort_all.py @@ -6,7 +6,7 @@ from common import sort_items if len(sys.argv) < 2: print("Sort the dictionary") - print("Usage: ", sys.argv[0], "[directory]") + print(("Usage: ", sys.argv[0], "[directory]")) exit(1) dirtectory = sys.argv[1] diff --git a/node/binding.cc b/node/binding.cc index 4fc18c3..7ae6bfa 100644 --- a/node/binding.cc +++ b/node/binding.cc @@ -1,25 +1,22 @@ #include -#include -#include #include #include "Config.hpp" #include "Converter.hpp" -using namespace v8; using namespace opencc; -string ToUtf8String(const Local& str) { - v8::String::Utf8Value utf8(str); - return std::string(*utf8); +string ToUtf8String(const v8::Local& val) { + Nan::Utf8String utf8(val); + return string(*utf8); } -class OpenccBinding : public node::ObjectWrap { +class OpenccBinding : public Nan::ObjectWrap { struct ConvertRequest { OpenccBinding* instance; string input; string output; - Persistent callback; + Nan::Callback *callback; Optional ex; ConvertRequest() @@ -42,42 +39,40 @@ class OpenccBinding : public node::ObjectWrap { } static NAN_METHOD(New) { - NanScope(); OpenccBinding* instance; try { - if (args.Length() >= 1 && args[0]->IsString()) { - string configFile = ToUtf8String(args[0]->ToString()); + if (info.Length() >= 1 && info[0]->IsString()) { + string configFile = ToUtf8String(info[0]); instance = new OpenccBinding(configFile); } else { instance = new OpenccBinding("s2t.json"); } } catch (opencc::Exception& e) { - NanThrowError(e.what()); - NanReturnUndefined(); + Nan::ThrowError(e.what()); + return; } - instance->Wrap(args.This()); - NanReturnValue(args.This()); + instance->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); } static NAN_METHOD(Convert) { - NanScope(); - if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsFunction()) { - NanThrowTypeError("Wrong arguments"); - NanReturnUndefined(); + if (info.Length() < 2 || !info[0]->IsString() || !info[1]->IsFunction()) { + Nan::ThrowTypeError("Wrong arguments"); + return; } ConvertRequest* conv_data = new ConvertRequest; - conv_data->instance = ObjectWrap::Unwrap(args.This()); - conv_data->input = ToUtf8String(args[0]->ToString()); - NanAssignPersistent(conv_data->callback, Local::Cast(args[1])); + conv_data->instance = Nan::ObjectWrap::Unwrap(info.This()); + conv_data->input = ToUtf8String(info[0]); + conv_data->callback = new Nan::Callback(info[1].As()); conv_data->ex = Optional::Null(); uv_work_t* req = new uv_work_t; req->data = conv_data; uv_queue_work(uv_default_loop(), req, DoConvert, (uv_after_work_cb)AfterConvert); - NanReturnUndefined(); + return; } static void DoConvert(uv_work_t* req) { @@ -91,61 +86,56 @@ class OpenccBinding : public node::ObjectWrap { } static void AfterConvert(uv_work_t* req) { - NanScope(); + Nan::HandleScope scope; ConvertRequest* conv_data = static_cast(req->data); - Local err = NanUndefined(); - Local converted = NanNew(conv_data->output.c_str()); + v8::Local err = Nan::Undefined(); + v8::Local converted = Nan::New(conv_data->output.c_str()).ToLocalChecked(); if (!conv_data->ex.IsNull()) { - err = NanNew(conv_data->ex.Get().what()); + err = Nan::New(conv_data->ex.Get().what()).ToLocalChecked(); } const unsigned argc = 2; - Local argv[argc] = { + v8::Local argv[argc] = { err, - NanNew(converted) + converted }; - NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(conv_data->callback), argc, argv); + conv_data->callback->Call(argc, argv); delete conv_data; delete req; } static NAN_METHOD(ConvertSync) { - NanScope(); - if (args.Length() < 1 || !args[0]->IsString()) { - NanThrowTypeError("Wrong arguments"); - NanReturnUndefined(); + if (info.Length() < 1 || !info[0]->IsString()) { + Nan::ThrowTypeError("Wrong arguments"); + return; } - OpenccBinding* instance = ObjectWrap::Unwrap(args.This()); + OpenccBinding* instance = Nan::ObjectWrap::Unwrap(info.This()); - string input = ToUtf8String(args[0]->ToString()); + string input = ToUtf8String(info[0]); string output; try { output = instance->Convert(input); } catch (opencc::Exception& e) { - NanThrowError(e.what()); - NanReturnUndefined(); + Nan::ThrowError(e.what()); + return; } - Local converted = NanNew(output.c_str()); - NanReturnValue(converted); + v8::Local converted = Nan::New(output.c_str()).ToLocalChecked(); + info.GetReturnValue().Set(converted); } - static void init(Handle target) { - NanScope(); + static NAN_MODULE_INIT(Init) { // Prepare constructor template - Local tpl = NanNew(OpenccBinding::New); - tpl->SetClassName(NanNew("Opencc")); + v8::Local tpl = Nan::New(OpenccBinding::New); + tpl->SetClassName(Nan::New("Opencc").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); // Prototype - NODE_SET_PROTOTYPE_METHOD(tpl, "convert", Convert); - NODE_SET_PROTOTYPE_METHOD(tpl, "convertSync", ConvertSync); + Nan::SetPrototypeMethod(tpl, "convert", Convert); + Nan::SetPrototypeMethod(tpl, "convertSync", ConvertSync); // Constructor - target->Set(NanNew("Opencc"), tpl->GetFunction()); + v8::Local cons = Nan::GetFunction(tpl).ToLocalChecked(); + Nan::Set(target, Nan::New("Opencc").ToLocalChecked(), cons); } }; -void init(Handle target) { - OpenccBinding::init(target); -} - -NODE_MODULE(binding, init); +NODE_MODULE(binding, OpenccBinding::Init); diff --git a/node/global.gypi b/node/global.gypi index e20ee6d..433f1b7 100644 --- a/node/global.gypi +++ b/node/global.gypi @@ -1,6 +1,6 @@ { "variables": { - "opencc_version": "1.0.3" + "opencc_version": "1.0.4" }, "target_defaults": { "defines": [ diff --git a/package.json b/package.json index 2895a54..f545798 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencc", - "version": "1.0.3", + "version": "1.0.4", "description": "Conversion between Traditional and Simplified Chinese", "author": "BYVoid ", "license": "Apache", @@ -24,9 +24,9 @@ "Traditional Chinese" ], "devDependencies": { - "mocha": "1.8.1" + "mocha": "2.2.5" }, "dependencies": { - "nan": "^1.6.2" + "nan": "^2.2.0" } } diff --git a/src/Config.cpp b/src/Config.cpp index cab6eff..35bc89b 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -16,6 +16,8 @@ * limitations under the License. */ +#include + #include "Config.hpp" #include "ConversionChain.hpp" #include "Converter.hpp" @@ -26,8 +28,6 @@ #include "document.h" -#include - using namespace opencc; typedef rapidjson::GenericValue> JSONValue; @@ -168,19 +168,19 @@ public: std::ifstream ifs; // Working directory - ifs.open(fileName.c_str()); + ifs.open(UTF8Util::GetPlatformString(fileName).c_str()); if (ifs.is_open()) { return fileName; } // Package data directory if (PACKAGE_DATA_DIRECTORY != "") { string prefixedFileName = PACKAGE_DATA_DIRECTORY + fileName; - ifs.open(prefixedFileName.c_str()); + ifs.open(UTF8Util::GetPlatformString(prefixedFileName).c_str()); if (ifs.is_open()) { return prefixedFileName; } prefixedFileName += ".json"; - ifs.open(prefixedFileName.c_str()); + ifs.open(UTF8Util::GetPlatformString(prefixedFileName).c_str()); if (ifs.is_open()) { return prefixedFileName; } @@ -197,7 +197,7 @@ Config::~Config() { delete (ConfigInternal*)internal; } ConverterPtr Config::NewFromFile(const string& fileName) { ConfigInternal* impl = (ConfigInternal*)internal; string prefixedFileName = impl->FindConfigFile(fileName); - std::ifstream ifs(prefixedFileName); + std::ifstream ifs(UTF8Util::GetPlatformString(prefixedFileName)); string content(std::istreambuf_iterator(ifs), (std::istreambuf_iterator())); diff --git a/src/SerializableDict.hpp b/src/SerializableDict.hpp index f0d52b0..a02e53e 100644 --- a/src/SerializableDict.hpp +++ b/src/SerializableDict.hpp @@ -47,7 +47,15 @@ public: template static bool TryLoadFromFile(const string& fileName, std::shared_ptr* dict) { - FILE* fp = fopen(fileName.c_str(), "rb"); + FILE* fp = +#ifdef _MSC_VER + // well, the 'GetPlatformString' shall return a 'wstring' + _wfopen(UTF8Util::GetPlatformString(fileName).c_str(), L"rb") +#else + fopen(UTF8Util::GetPlatformString(fileName).c_str(), "rb") +#endif // _MSC_VER + ; + if (fp == NULL) { return false; } diff --git a/src/SimpleConverter.cpp b/src/SimpleConverter.cpp index 90f412b..b3dfb60 100644 --- a/src/SimpleConverter.cpp +++ b/src/SimpleConverter.cpp @@ -16,6 +16,12 @@ * limitations under the License. */ +#ifdef _MSC_VER +#define NOMINMAX +#include +#undef NOMINMAX +#endif // _MSC_VER + #include "Config.hpp" #include "Converter.hpp" #include "opencc.h" @@ -82,7 +88,7 @@ size_t SimpleConverter::Convert(const char* input, size_t length, static string cError; -opencc_t opencc_open(const char* configFileName) { +opencc_t opencc_open_internal(const char* configFileName) { try { if (configFileName == nullptr) { configFileName = OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD; @@ -95,6 +101,37 @@ opencc_t opencc_open(const char* configFileName) { } } +#ifdef _MSC_VER +opencc_t opencc_open_w(const wchar_t* configFileName) { + try { + if (configFileName == nullptr) { + return opencc_open_internal(nullptr); + } + std::string utf8fn = UTF8Util::U16ToU8(configFileName); + return opencc_open_internal(utf8fn.c_str()); + } catch (std::runtime_error& ex) { + cError = ex.what(); + return reinterpret_cast(-1); + } +} +opencc_t opencc_open(const char* configFileName) { + if (configFileName == nullptr) { + return opencc_open_internal(nullptr); + } + std::wstring wFileName; + int convcnt = MultiByteToWideChar(CP_ACP, 0, configFileName, -1, NULL, 0); + if (convcnt > 0) { + wFileName.resize(convcnt); + MultiByteToWideChar(CP_ACP, 0, configFileName, -1, &wFileName[0], convcnt); + } + return opencc_open_w(wFileName.c_str()); +} +#else +opencc_t opencc_open(const char* configFileName) { + return opencc_open_internal(configFileName); +} +#endif + int opencc_close(opencc_t opencc) { try { SimpleConverter* instance = reinterpret_cast(opencc); diff --git a/src/UTF8Util.hpp b/src/UTF8Util.hpp index a662290..ea03dc0 100644 --- a/src/UTF8Util.hpp +++ b/src/UTF8Util.hpp @@ -18,6 +18,12 @@ #pragma once +#ifdef _MSC_VER +#define NOMINMAX +#include +#undef NOMINMAX +#endif // _MSC_VER + #include "Common.hpp" namespace opencc { @@ -241,5 +247,38 @@ public: pstr = NextChar(pstr); } } + +#ifdef _MSC_VER + static std::wstring GetPlatformString(const std::string& str) { + return U8ToU16(str); + } +#else + static std::string GetPlatformString(const std::string& str) { + return str; + } +#endif // _MSC_VER + + +#ifdef _MSC_VER + static std::string U16ToU8(const std::wstring& wstr) { + std::string ret; + int convcnt = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL); + if (convcnt > 0) { + ret.resize(convcnt); + WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), &ret[0], convcnt, NULL, NULL); + } + return ret; + } + + static std::wstring U8ToU16(const std::string& str) { + std::wstring ret; + int convcnt = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0); + if (convcnt > 0) { + ret.resize(convcnt); + MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &ret[0], convcnt); + } + return ret; + } +#endif // _MSC_VER }; } diff --git a/src/opencc.h b/src/opencc.h index f180ce2..21b9046 100644 --- a/src/opencc.h +++ b/src/opencc.h @@ -71,6 +71,18 @@ typedef void* opencc_t; * @ingroup opencc_c_api */ OPENCC_EXPORT opencc_t opencc_open(const char* configFileName); +#ifdef _MSC_VER +/** +* Makes an instance of opencc (wide char / Unicode) +* +* @param configFileName Location of configuration file. If this is set to NULL, +* OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD will be loaded. +* @return A description pointer of the newly allocated instance of +* opencc. On error the return value will be (opencc_t) -1. +* @ingroup opencc_c_api +*/ +OPENCC_EXPORT opencc_t opencc_open_w(const wchar_t* configFileName); +#endif /* _MSC_VER */ /** * Destroys an instance of opencc